﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Anchor.Miscellaneous
{
    public class Request
    {
        private int mHeaderID;
        private string mHeader;
        private string mContent;
        public int reader;

        public int HeaderID { get { return mHeaderID; } }
        public string Header { get { return mHeader; } set { mHeader = value; } }
        public string Content { get { return mContent; } set { mContent = value; } }
        public string GetRestContent { get { return mContent.Substring(reader); } }

        public static Request Parse(string Data)
        {
            Request request = new Request();

            request.reader = 0;
            request.mHeaderID = hEncoding.decodeB64(Data.Substring(0, 2));
            request.mHeader = Data.Substring(0, 2);
            request.mContent = Data.Substring(2);

            return request;
        }

        public int PopBase64Int()
        {
            int i = hEncoding.decodeB64(mContent.Substring(reader, 2));
            reader += 2;

            return i;
        }
        public uint PopBase64UInt()
        {
            return (uint)PopBase64Int();
        }
        public int PopWiredInt()
        {
            int i = hEncoding.decodeWired(mContent.Substring(reader));
            reader += hEncoding.encodeB64(i).Length;

            return i;
        }
        public uint PopWiredUInt()
        {
            return (uint)PopWiredInt();
        }
        public byte PopWiredByte()
        {
            return (byte)PopWiredInt();
        }
        public string PopString()
        {
            int length = hEncoding.decodeB64(mContent.Substring(reader, 2));
            string s = mContent.Substring(2 + reader, length);
            reader += 2 + length;

            return s;
        }
        public bool PopWiredBoolean()
        {
            int i = hEncoding.decodeWired(mContent.Substring(reader));
            reader += hEncoding.encodeWired(i).Length;

            if (i == 1)
                return true;
            else
                return false;
        }
        public bool PopBase64Boolean()
        {
            int i = hEncoding.decodeB64(mContent.Substring(reader++, 1));
            if (i == 1)
                return true;
            else
                return false;
        }
        public string PopArgument()
        {
            return PopArgument('/');
        }
        public string PopArgument(char c)
        {
            try
            {
                string s = mContent.Substring(reader, GetRestContent.IndexOf(c));
                reader += s.Length + 1;

                return s;
            }
            catch { return GetRestContent; }
        }
        public bool PopBooleanArgument()
        {
            return PopArgument() == "1";
        }
        public bool PopBooleanArgument(char c)
        {
            return PopArgument(c) == "1";
        }
        public int PopIntArgument()
        {
            return Convert.ToInt32(PopArgument());
        }
        public int PopIntArgument(char c)
        {
            return Convert.ToInt32(PopArgument(c));
        }
    }
}
